package com.sinopec.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.sinopec.android.bean.BusinessTripBean;
import com.sinopec.android.bean.DocumentBean;
import com.sinopec.android.bean.KqglBasic;
import com.sinopec.android.bean.ProcessIdea;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.SubmitBusinessTripBean;
import com.sinopec.android.bean.Thing;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.constant.Constants;
import com.sinopec.android.net.ThreadManger;
import com.sinopec.android.util.AsyncImageLoader;
import com.sinopec.android.util.DateUtil;
import com.sinopec.android.util.SharePreferencesUtil;
import com.sinopec.android.util.StringUtil;
import com.sinopec.android.util.ToastUtil;
import com.sinopec.android.util.AsyncImageLoader.ImageCallback;

/**
* @ClassName: BusinessTripActivity
* @Description: TODO(出差公文详情)
* @author liuwei
* @date 2012-4-16 下午02:36:01
* 
*/
public class BusinessTripActivity extends BaseActivity implements
		OnClickListener, ThreadCallBack {
	
	Thing thing;
	private ScrollView document_scroll_layout;
	private TextView document_chuchairen_textview,document_suozaibumen_textview
	,document_chuchaikaishishijian_textview,document_chuchaididian_textview,document_chuchaishiyou_textview
	,document_chengcheluxian_textview;
	private TextView process_idea_zero_textview,process_idea_first_textview,process_idea_second_textview;
	private LinearLayout zero_layout,first_layout,second_layout,process_idea_zero_layout,process_idea_first_layout,process_idea_second_layout,shenpi_layout;
	BusinessTripBean businessTripBean;
	ArrayList<ProcessIdea> processIdeaList;
	KqglBasic kqglBasic;
	
	LayoutInflater inflater;
	
	EditText  fillEditText;
	Button submitbtn;
	Button rollbackbtn;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_trip_view);
		thing = (Thing) getIntent().getSerializableExtra("thing");
		initView();
		//调接口
		sendToServer();
	}

	void initView(){
		inflater = LayoutInflater.from(BusinessTripActivity.this);
		((TextView)findViewById(R.id.navi)).setText(thing.getType());
		document_scroll_layout = (ScrollView) findViewById(R.id.document_scroll_layout);
		document_scroll_layout.setVisibility(View.GONE);
		//出差人
		document_chuchairen_textview = (TextView) findViewById(R.id.document_chuchairen_textview);
		//所在部门
		document_suozaibumen_textview = (TextView) findViewById(R.id.document_suozaibumen_textview);
		//出差开始时间
		document_chuchaikaishishijian_textview = (TextView) findViewById(R.id.document_chuchaikaishishijian_textview);
		//出差地点
		document_chuchaididian_textview =  (TextView) findViewById(R.id.document_chuchaididian_textview);
		//出差事由
		document_chuchaishiyou_textview = (TextView) findViewById(R.id.document_chuchaishiyou_textview);
		//出差路线
		document_chengcheluxian_textview = (TextView) findViewById(R.id.document_chengcheluxian_textview);
		//部门负责人意见
		zero_layout = (LinearLayout) findViewById(R.id.zero_layout);
		process_idea_zero_textview = (TextView) findViewById(R.id.process_idea_zero_textview);
		process_idea_zero_layout = (LinearLayout) findViewById(R.id.process_idea_zero_layout);
		
		first_layout = (LinearLayout) findViewById(R.id.first_layout);
		//第一个领导意见
		process_idea_first_textview = (TextView) findViewById(R.id.process_idea_first_textview);
		process_idea_first_layout = (LinearLayout) findViewById(R.id.process_idea_first_layout);
		
		second_layout = (LinearLayout) findViewById(R.id.second_layout);
		//第二个领导意见
		process_idea_second_textview = (TextView) findViewById(R.id.process_idea_second_textview);
		process_idea_second_layout = (LinearLayout) findViewById(R.id.process_idea_second_layout);
		
		//审批区域
		shenpi_layout = (LinearLayout) findViewById(R.id.shenpi_layout);
		fillEditText = (EditText) findViewById(R.id.shenpiyijian_edittext);
		submitbtn = (Button) findViewById(R.id.submit_btn);
		submitbtn.setOnClickListener(this);
		rollbackbtn = (Button) findViewById(R.id.rollback_btn);
		rollbackbtn.setOnClickListener(this);
		if(thing.isYiBan()){
			shenpi_layout.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onCallbackFromThread(ResultData resultData) {
		// TODO Auto-generated method stub
		if(resultData!=null && resultData.getResultList()!=null){
		}else{
			Toast.makeText(this,R.string.get_fail, 0).show();
			return;
		}
		if(resultData.getResultList().get(0) instanceof String){
			Toast.makeText(this,resultData.getResultList().get(0).toString(), 0).show();
			return ;
		}
		ArrayList arrayList = resultData.getResultList();
		//出差公文结果
		if(arrayList.size()>0&&arrayList.get(0) instanceof BusinessTripBean){
			document_scroll_layout.setVisibility(View.VISIBLE);//设置可见
			businessTripBean = (BusinessTripBean) arrayList.get(0);
			if(businessTripBean!=null){
				processIdeaList = businessTripBean.getProcessIdeaList();
				kqglBasic = businessTripBean.getKqglBasic();
				if(kqglBasic!=null){
				//公文基本信息
					document_chuchairen_textview.setText(kqglBasic.getEvectionUserName());
					document_suozaibumen_textview.setText(kqglBasic.getEvectionDeport());
					document_chuchaikaishishijian_textview.setText(kqglBasic.getEvectionStartTime()+"至"+kqglBasic.getEvectionEndTime());
//					document_chuchaijieshushijian_textview.setText(kqglBasic.getEvectionEndTime()+" "+DateUtil.getWeek(kqglBasic.getEvectionEndTime()));
					document_chuchaididian_textview.setText(kqglBasic.getEvectionAddress());
					document_chuchaishiyou_textview.setText(kqglBasic.getEvectionReason());
					document_chengcheluxian_textview.setText(kqglBasic.getEvectionRideLine());
				}
				//正职   1 分管院领导意见   2院领导意见 
				//副职   0 部门负责人意见  1 分管院领导意见 2院领导意见 
				//员工   0 部门负责人意见
				if(processIdeaList!=null &&processIdeaList.size()>0){
					if(thing.getLevelNum()==0){//正职
							for (int i = 0; i < processIdeaList.size(); i++) {
								ProcessIdea processIdea = processIdeaList.get(i);
								
								if(processIdea!=null){
									View view = inflater.inflate(R.layout.process_idea_item_layout, null);
									TextView yjTextview = (TextView) view.findViewById(R.id.process_idea_yijian_textview);
									yjTextview.setText(processIdea.getSignComment());
									
									TextView timeTextview = (TextView) view.findViewById(R.id.process_idea_time_textview);
									timeTextview.setText(processIdea.getSignTime());
									
									TextView ldTextview = (TextView) view.findViewById(R.id.process_idea_lingdao_textview);
									if(StringUtil.isNotEmpty(processIdea.getSignUserName())){
										ldTextview.setText(processIdea.getSignUserName());
									}
									
									LinearLayout sign_in_layout = (LinearLayout) view.findViewById(R.id.sign_in_layout);
									if(StringUtil.isNotEmpty(processIdea.getSignUserPath())){
										sign_in_layout.setVisibility(View.VISIBLE);
//										ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
//										signImageView.setImageDrawable(getResources().getDrawable(Constants.getSignInImage(processIdea.getSignUserPath())));
									
										final	ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
										String url = processIdea.getSignUserPath();
										Bitmap bitmap = AsyncImageLoader.loadDrawable(url, new ImageCallback() {
											@Override
											public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
//													ImageView imageView = (ImageView)listView.findViewWithTag(imageUrl);
													if(imageDrawable!=null ){
//														imageDrawable = ImageUtil.getRoundedCornerBitmap(imageUrl,imageDrawable, 10.0f);
//														imageView.setImageBitmap(imageDrawable);
														signImageView.setBackgroundDrawable(new BitmapDrawable(imageDrawable));
													}
											}
										});
										
										if(bitmap!=null){
											signImageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
										}
									
									}
									LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
											LinearLayout.LayoutParams.FILL_PARENT, 8);
									TextView textView = new TextView(this);
									textView.setLayoutParams(layoutParams);
									
									if(processIdea.getJobStep()==1){
										process_idea_first_textview.setText(getResources().getString(R.string.fenguanyuanlingdaoyijian_text));
										process_idea_first_layout.addView(view);
										process_idea_first_layout.addView(textView);
										first_layout.setVisibility(View.VISIBLE);
									}
									if(processIdea.getJobStep()==2){
										process_idea_second_textview.setText(getResources().getString(R.string.yuanlingdaoyijian_text));
										process_idea_second_layout.addView(view);
										process_idea_second_layout.addView(textView);
										second_layout.setVisibility(View.VISIBLE);
									}
									
									
								}
							}	
					}else if(thing.getLevelNum()==1){//副职
						for (int i = 0; i < processIdeaList.size(); i++) {
							ProcessIdea processIdea = processIdeaList.get(i);
							
							if(processIdea!=null){
								View view = inflater.inflate(R.layout.process_idea_item_layout, null);
								TextView yjTextview = (TextView) view.findViewById(R.id.process_idea_yijian_textview);
								yjTextview.setText(processIdea.getSignComment());
								
								TextView timeTextview = (TextView) view.findViewById(R.id.process_idea_time_textview);
								timeTextview.setText(processIdea.getSignTime());
								
								TextView ldTextview = (TextView) view.findViewById(R.id.process_idea_lingdao_textview);
								if(StringUtil.isNotEmpty(processIdea.getSignUserName())){
									ldTextview.setText(processIdea.getSignUserName());
								}
								
								LinearLayout sign_in_layout = (LinearLayout) view.findViewById(R.id.sign_in_layout);
								if(StringUtil.isNotEmpty(processIdea.getSignUserPath())){
									sign_in_layout.setVisibility(View.VISIBLE);
//									ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
//									signImageView.setImageDrawable(getResources().getDrawable(Constants.getSignInImage(processIdea.getSignUserPath())));
									final	ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
									String url = processIdea.getSignUserPath();
									Bitmap bitmap = AsyncImageLoader.loadDrawable(url, new ImageCallback() {
										@Override
										public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
//												ImageView imageView = (ImageView)listView.findViewWithTag(imageUrl);
												if(imageDrawable!=null ){
//													imageDrawable = ImageUtil.getRoundedCornerBitmap(imageUrl,imageDrawable, 10.0f);
//													imageView.setImageBitmap(imageDrawable);
													signImageView.setBackgroundDrawable(new BitmapDrawable(imageDrawable));
												}
										}
									});
									
									if(bitmap!=null){
										signImageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
									}
								}
								LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
										LinearLayout.LayoutParams.FILL_PARENT, 8);
								TextView textView = new TextView(this);
								textView.setLayoutParams(layoutParams);
								if(processIdea.getJobStep()==0){
									process_idea_zero_textview.setText(getResources().getString(R.string.bumenfuzerenyijian_text));
									process_idea_zero_layout.addView(view);
									process_idea_zero_layout.addView(textView);
									zero_layout.setVisibility(View.VISIBLE);
								}
								if(processIdea.getJobStep()==1){
									process_idea_first_textview.setText(getResources().getString(R.string.fenguanyuanlingdaoyijian_text));
									process_idea_first_layout.addView(view);
									process_idea_first_layout.addView(textView);
									first_layout.setVisibility(View.VISIBLE);
								}
								if(processIdea.getJobStep()==2){
									process_idea_second_textview.setText(getResources().getString(R.string.yuanlingdaoyijian_text));
									process_idea_second_layout.addView(view);
									process_idea_second_layout.addView(textView);
									second_layout.setVisibility(View.VISIBLE);
								}
								
							}
						}	
					}else{
						
						//员工
						for (int i = 0; i < processIdeaList.size(); i++) {
							ProcessIdea processIdea = processIdeaList.get(i);
							
							if(processIdea!=null){
								View view = inflater.inflate(R.layout.process_idea_item_layout, null);
								TextView yjTextview = (TextView) view.findViewById(R.id.process_idea_yijian_textview);
								yjTextview.setText(processIdea.getSignComment());
								
								TextView timeTextview = (TextView) view.findViewById(R.id.process_idea_time_textview);
								timeTextview.setText(processIdea.getSignTime());
								
								TextView ldTextview = (TextView) view.findViewById(R.id.process_idea_lingdao_textview);
								if(StringUtil.isNotEmpty(processIdea.getSignUserName())){
									ldTextview.setText(processIdea.getSignUserName());
								}
								
								LinearLayout sign_in_layout = (LinearLayout) view.findViewById(R.id.sign_in_layout);
								if(StringUtil.isNotEmpty(processIdea.getSignUserPath())){
									sign_in_layout.setVisibility(View.VISIBLE);
//									ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
//									signImageView.setImageDrawable(getResources().getDrawable(Constants.getSignInImage(processIdea.getSignUserPath())));
									final	ImageView signImageView = (ImageView) view.findViewById(R.id.sign_in_image_view);
									String url = processIdea.getSignUserPath();
									Bitmap bitmap = AsyncImageLoader.loadDrawable(url, new ImageCallback() {
										@Override
										public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
//												ImageView imageView = (ImageView)listView.findViewWithTag(imageUrl);
												if(imageDrawable!=null ){
//													imageDrawable = ImageUtil.getRoundedCornerBitmap(imageUrl,imageDrawable, 10.0f);
//													imageView.setImageBitmap(imageDrawable);
													signImageView.setBackgroundDrawable(new BitmapDrawable(imageDrawable));
												}
										}
									});
									
									if(bitmap!=null){
										signImageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
									}
								}
								LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
										LinearLayout.LayoutParams.FILL_PARENT, 8);
								TextView textView = new TextView(this);
								textView.setLayoutParams(layoutParams);
								
//								zero_layout.setVisibility(View.VISIBLE);
//								process_idea_zero_textview.setText(getResources().getString(R.string.bumenfuzerenyijian_text));
//								process_idea_zero_layout.addView(view);
//								process_idea_zero_layout.addView(textView);
								process_idea_second_textview.setText(getResources().getString(R.string.bumenfuzerenyijian_text));
								process_idea_second_layout.addView(view);
								process_idea_second_layout.addView(textView);
								second_layout.setVisibility(View.VISIBLE);
								
								
							}
						}	
					
					}
				}else{
					if(thing.getLevelNum()==0){//正职
						process_idea_first_textview.setText(getResources().getString(R.string.fenguanyuanlingdaoyijian_text));
						process_idea_second_textview.setText(getResources().getString(R.string.yuanlingdaoyijian_text));
					}else if(thing.getLevelNum()==1){//副职
						process_idea_first_textview.setText(getResources().getString(R.string.fenguanyuanlingdaoyijian_text));
						process_idea_second_textview.setText(getResources().getString(R.string.yuanlingdaoyijian_text));
					}else{//员工
						first_layout.setVisibility(View.GONE);
						process_idea_second_textview.setText(getResources().getString(R.string.bumenfuzerenyijian_text));
					}
					
					
				}
				
				//审批意见区
				if("0".equals(businessTripBean.getBtnRetrieve())){
					//隐藏回退按钮
					rollbackbtn.setVisibility(View.GONE);
				}else if("1".equals(businessTripBean.getBtnRetrieve())){
					//显示回退按钮
					rollbackbtn.setVisibility(View.VISIBLE);
				}
			}
				
		}
		
		//提交或回退出差公文
		if(arrayList.size()>0&&arrayList.get(0) instanceof SubmitBusinessTripBean){
			if("0".equals(Constants.btnClickType)){
				ToastUtil.showToast(getApplicationContext(), "您的签批已经成功");
			}else if("1".equals(Constants.btnClickType)){
				ToastUtil.showToast(getApplicationContext(), "出差申请回退成功");
			}
			//跳转至前一页面
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), ToDoThingActivity.class);
			startActivity(intent);
			this.finish();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.submit_btn://提交意见
			if(StringUtil.isEmpty(fillEditText.getText().toString())){
				ToastUtil.showToast(BusinessTripActivity.this, "请填写意见");
				fillEditText.setText(null);
				return ;
			}
			Constants.btnClickType = "0";
			submitToServer();
			break;
		case R.id.rollback_btn://回退意见
			if(StringUtil.isEmpty(fillEditText.getText().toString())){
				ToastUtil.showToast(BusinessTripActivity.this, "请填写意见");
				fillEditText.setText(null);
				return ;
			}
			Constants.btnClickType = "1";
			submitToServer();
			break;
		default:
			break;
		}
	}
	
	void  sendToServer(){
		String reqUrl =ConfigAttrs.BASE_URL+"GetSuperiorDocument.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT));
		hashMap.put("passWord", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD));
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);
		hashMap.put("InsID", thing.getUrl());
		hashMap.put("bussinessID", thing.getBussinessID());
		hashMap.put("btnType", "1");

		ThreadManger.exeTask(this, ConfigAttrs.BUSINESS_TRIP_TASK_FLAG, hashMap, reqUrl);
	}
	//点提交或退回按钮
	void submitToServer(){
		String reqUrl =ConfigAttrs.BASE_URL+"SubmitKqgl.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT));
		hashMap.put("passWord", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD));
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);
		hashMap.put("InsID", businessTripBean.getInsID());
		hashMap.put("signComment", fillEditText.getText().toString());
		hashMap.put("btnClickType", Constants.btnClickType);

		ThreadManger.exeTask(this, ConfigAttrs.SUBMIT_BUSINESS_TRIP_TASK_FLAG, hashMap, reqUrl);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			//跳转至前一页面
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), BusinessTripListActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return true;
	}
}
